package com.gxa.admin.controller;

import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.jwt.ParseTokenUtils;
import com.gxa.modules.pojo.LoanSearch;
import com.gxa.modules.pojo.Reconciliation;
import com.gxa.modules.pojo.User;
import com.gxa.modules.service.ParameterService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/financial")
@Api(value = "财务台账test",tags = "财务台账接口API--周军国")
public class ParameterController {
    @Autowired(required = false)
    private ParameterService parameterService;
    @PostMapping("/selectPrincipal")
    @ApiOperation(value = "查询财务台账",notes = "查询条件实体实体",httpMethod = "POST", response = JsonResult.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏请求"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=405,message="Method请求错误"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public JsonResult selectPrincipalFinancial(@RequestBody PageRecv<LoanSearch> pageRecv){
        JsonResult jsonResult=new JsonResult();
        jsonResult=parameterService.selectPrincipalFinancial(pageRecv);
        return jsonResult;
    }
    @GetMapping("/Reconciliation")
    @ApiOperation(value = "查询具体对账信息",notes = "具体台账信息实体",httpMethod = "GET", response = JsonResult.class)
            @ApiImplicitParam(name = "reconciliation_id",value = "台账主键id",paramType = "query",dataType = "String", required = false)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏请求"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=405,message="Method请求错误"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public JsonResult selectReconciliationInformation(long reconciliation_id){
        JsonResult jsonResult=new JsonResult();
        jsonResult=parameterService.selectReconciliationInformation(reconciliation_id);
        return jsonResult;
    }
    @PostMapping ("/updateReconciliation")
    @ApiOperation(value = "更新对账信息",notes = "具体台账信息实体",httpMethod = "POST", response = ResultVo.class)
    @ApiResponses({
            @ApiResponse(code=200,message="请求成功"),
            @ApiResponse(code=400,message="坏请求"),
            @ApiResponse(code=404,message="找不到页面"),
            @ApiResponse(code=405,message="Method请求错误"),
            @ApiResponse(code=500,message="后台服务错误")
    })
    public JsonResult updateReconciliation(@RequestBody Reconciliation reconciliation,HttpServletRequest request){
        User user=ParseTokenUtils.getCurrentUser(request);
        reconciliation.setReconciliation_name(user.getUser_name());
        JsonResult jsonResult=new JsonResult();
        jsonResult=parameterService.updateReconciliation(reconciliation);
        jsonResult.setMsg("更新成功");
        return  jsonResult;
    }

//    接收凭证文件
    @GetMapping("/receiveFile")
    public JsonResult receiveFile(List<MultipartFile> fileList, HttpServletRequest request) throws IOException {
        JsonResult jsonResult=new JsonResult();
        List<String> urlList=new ArrayList<>();
        if(fileList.isEmpty()){
            jsonResult.setCode(500);
            jsonResult.setMsg("没有选中文件");
        }else{
        String uploadPath=request.getServletContext().getRealPath("")+ File.separator+"upload"+File.separator;
            File file=new File(uploadPath);
            if(!file.exists()){
                file.mkdirs();
            }
            for(MultipartFile file1:fileList){
                String fileName=file1.getOriginalFilename();
                System.out.println("文件名"+fileName);
                File newFile=new File(uploadPath+fileName);
                System.out.println("整体地址"+newFile.toString());
//                上传
                FileCopyUtils.copy(file1.getBytes(),newFile);
                urlList.add(File.separator+"upload"+File.separator+fileName);
            }
        }
        jsonResult.setCode(200);
        jsonResult.setMsg("上传成功");
        jsonResult.setData(urlList);
    return jsonResult;
    }

//    本金收款台账，
    @PostMapping("/selectCollection")
public JsonResult selectCollection(@RequestBody PageRecv<LoanSearch> pageRecv){
        JsonResult jsonResult=new JsonResult();
        jsonResult=parameterService.selectCollection(pageRecv);
        return jsonResult;
}
//批量处理
    @PostMapping("/updateCollectionAll")
    public JsonResult selectCollectionAll(@RequestParam("ids") List<Integer> ids,@RequestParam("reconciliation_name") String reconciliation_name){
        JsonResult jsonResult=new JsonResult();
        jsonResult=parameterService.updateCollectionAll(ids,reconciliation_name);
        return  jsonResult;
    }
}
